package com.chaopei.znpk.service.impl;

import com.chaopei.znpk.entity.Teacher;
import com.chaopei.znpk.mapper.TeacherMapper;
import com.chaopei.znpk.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.*;


/**
* @author 28458
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2022-05-18 10:49:47
*/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherMapper teacherMapper;

    @Override
    public void queryTeacherFenpei(String teacher_id, Integer fenpei) {
        teacherMapper.queryTeacherFenpei(teacher_id,fenpei);
    }

    /**
     * 1.批量添加老师
     * @param list 老师列表
     * @return 操作结果信息
     */
    @Override
    public Map<String, Object> addList(List<Teacher> list) {
        int error = 0;          // 添加失败的行数
        int success = 0;
        List<Teacher> successList = new ArrayList<>();  // 添加成功的信息
        Map<String,Object> map = new HashMap<>();

        for (Teacher teacher : list) {
            int i = 0;
            try {
//                teacher.setTeacherId( );
                System.out.println(teacher.toString());
                i = teacherMapper.addTeacher(teacher);
                System.out.println(i+"就是结果");
            } catch (Exception e) {
                e.printStackTrace();
                if (e.getMessage().contains("SQLIntegrityConstraintViolationException")) {
                    map.put("errorMsg","主键冲突");
                } else {
                    map.put("errorMsg",e.getMessage());
                }
            }
            if (i > 0) {
                success += 1;
                successList.add(teacher);
            } else {
                error += 1;
            }
        }
        map.put("successLine",success);             // 添加失败的行数
        map.put("errorLine",error);                 // 成功添加的行数
        map.put("successList",successList);     // 添加成功的信息
        return map;
    }

    /**
     * 2.添加一个教师信息
     * @param teacher
     * @return
     */
    @Override
    public Map<String, Object> addOneTeacher(Teacher teacher) {
        Map<String,Object> map = new HashMap<>();
        int i = 0;
        try {
            i = teacherMapper.addTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains("SQLIntegrityConstraintViolationException")) {
                map.put("errorMsg","主键冲突");
            } else {
                map.put("errorMsg",e.getMessage());
            }
        }
        if (i > 0 ) {
            map.put("result", "添加成功！");
        } else {
            map.put("result","添加失败！");
        }
        return map;
    }

    /**
     * 3.teacher表的条件查询（当条件为空时查询所有）
     * @param teacher
     * @return
     */
    @Override
    public Map<String, Object> getTeacherList(Teacher teacher) {
        List<Teacher> list = teacherMapper.getTeacherList(teacher);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }   

    
}




